package com.example.app

import android.Manifest
import android.app.Application
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.OnBackPressedCallback
import androidx.activity.compose.setContent
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.view.WindowCompat
import com.amap.api.maps.MapsInitializer
import com.example.app.components.MyPlaceSelected.MyMapSelectedState
import com.example.app.pages.main.Main
import com.example.app.pages.main.home.accountList.viewModel.AccountListViewModel
import com.example.app.pages.main.home.addNew.addRecord.viewModel.AddRecordViewModel
import com.example.app.pages.main.home.me.viewModel.AddNewViewModel
import com.example.app.pages.main.home.viewModel.HomeViewModel
import com.example.app.pages.main.login.viewModel.LoginViewModel
import com.example.app.ui.theme.AppTheme
import com.example.app.viewmodel.BarStateViewModel
import com.example.app.viewmodel.MainViewModel
import com.example.app.viewmodel.RouteViewModel
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberMultiplePermissionsState
import com.tencent.mmkv.MMKV

var routeViewModel = RouteViewModel()
var mainViewModel = MainViewModel(
    routeViewModel = routeViewModel
)
var myMapSelectedState = MyMapSelectedState()
var homeViewModel = HomeViewModel(mainViewModel = mainViewModel)
var addNewViewModel = AddNewViewModel(mainViewModel = mainViewModel)
var loginViewModel = LoginViewModel(mainViewModel = mainViewModel)
var meViewModel = AddNewViewModel(mainViewModel = mainViewModel)
var accountListViewModel = AccountListViewModel(mainViewModel = mainViewModel)
var barStateViewModel = BarStateViewModel(mainViewModel = mainViewModel)
var addRecordViewModel = AddRecordViewModel(mainViewModel)
class MainActivity : ComponentActivity() {
    lateinit var mainViewModel: MainViewModel
    private var exitTime = 0L
    @OptIn(ExperimentalPermissionsApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MapsInitializer.setApiKey("66468733a5212f35d8219dbc179395f6")
        MapsInitializer.debugMode(true)
        Log.d(
            "easy",
            "------>${BuildConfig.ENV}<------"
        )
        WindowCompat.setDecorFitsSystemWindows(
            window,
            false
        )
        //此调用会请求您的应用显示在系统界面后面。然后，您的应用将控制如何使用这些边衬区来调整界面。
        //https://developer.android.com/develop/ui/compose/layouts/insets?hl=zh-cn
        //enableEdgeToEdge()
        MMKV.initialize(this.filesDir.absolutePath + "/mmkv")
        var application = applicationContext as Application
        setContent {
            AppTheme(
            ) {
                //初始化权限
                val permissionState = rememberMultiplePermissionsState(
                    permissions = listOf(Manifest.permission.ACCESS_FINE_LOCATION)
                )

                DisposableEffect(permissionState.allPermissionsGranted) {
                    if (!permissionState.allPermissionsGranted) {
                        //权限申请
                        permissionState.launchMultiplePermissionRequest()
                    }

                    onDispose {
                    }
                }


                if (permissionState.allPermissionsGranted) {
                    Main()
                }
            }
        }
        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                if (System.currentTimeMillis() - exitTime > 2000) {
                    exitTime = System.currentTimeMillis()
                    Toast.makeText(
                        this@MainActivity,
                        "再按一次返回桌面",
                        Toast.LENGTH_SHORT
                    )
                        .show()
                } else {
                    moveTaskToBack(true)
                }
            }
        })
    }
}
@Composable
fun Greeting(
        name: String,
        modifier: Modifier = Modifier
) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    AppTheme {
        Greeting("Android")
    }
}